package com.GlobalPaint.app.ui.Home.Palette;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.GlobalPaint.app.Base.DialogWaiting;
import com.GlobalPaint.app.R;
import com.GlobalPaint.app.bean.PaletteBean;
import com.GlobalPaint.app.bean.ReturnStatusResultEntity;
import com.GlobalPaint.app.utils.Constants;
import com.GlobalPaint.app.utils.DataManager;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.List;

public class PaletteActivity extends AppCompatActivity implements View.OnClickListener{

    private XRecyclerView palette_xrecyler;
    private LinearLayoutManager linearLayoutManager;
    private List<PaletteBean.DataBean> list;
    private ImageView iv_goback;
    private TextView tv_common_center;
    private int page;
    private Dialog dlgWaiting2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_palette);
        initView();
    }

    private void initView() {
        iv_goback = (ImageView) findViewById(R.id.iv_goback);
        iv_goback.setOnClickListener(this);
        tv_common_center = (TextView) findViewById(R.id.tv_common_center);
        if (DataManager.typeId==121){
            tv_common_center.setText("色品样板");
        }else if(DataManager.typeId==114){
            tv_common_center.setText("行业动态咨询");
        }else if(DataManager.typeId==124){
            tv_common_center.setText("企业展示");
        }else if(DataManager.typeId==127){
            tv_common_center.setText("水性木器漆专区");
        }else if(DataManager.typeId==150){
            tv_common_center.setText("涂料产品推广专区");
        }

        palette_xrecyler = (XRecyclerView) findViewById(R.id.palette_xrecyler);
        linearLayoutManager = new LinearLayoutManager(getApplicationContext());
        palette_xrecyler.setLayoutManager(linearLayoutManager);
        getList();
        initLoad();
    }
    private void getList(){
        dlgWaiting2 = DialogWaiting.createDialogWaiting(PaletteActivity.this, "加载中......");
        dlgWaiting2.show();
        String url= Constants.JASON_SERVICE_URL+"info/getList?fNodeId="+ DataManager.typeId;
        DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), PaletteBean.class, new DataManager.Callback() {
            @Override
            public void onSuccess(Object o) {
                dlgWaiting2.cancel();
                PaletteBean paletteBean=(PaletteBean)o;
                if (paletteBean.getStatus() == 1){
                    if(paletteBean.getData() != null && paletteBean.getData().size()>0){
                        list=paletteBean.getData();

                    }
                    else {
                        Toast.makeText(PaletteActivity.this,"获取失败",Toast.LENGTH_LONG).show();
                    }
                }
                else {
                    Toast.makeText(PaletteActivity.this,"获取失败",Toast.LENGTH_LONG).show();
                }
                palette_xrecyler.refreshComplete();
            }

            @Override
            public void onFailure(ReturnStatusResultEntity error) {
                dlgWaiting2.cancel();
                    if(list!=null && list.size()>0) {
                        list.clear();
                        Toast.makeText(PaletteActivity.this, "获取失败", Toast.LENGTH_LONG).show();

                        palette_xrecyler.refreshComplete();

                }
            }
        });
        dlgWaiting2.cancel();
    }
    private void initLoad() {
        palette_xrecyler.setLoadingMoreEnabled(false);
        palette_xrecyler.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                String url= Constants.JASON_SERVICE_URL+"info/getList?fNodeId="+ DataManager.typeId;
                DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), PaletteBean.class, new DataManager.Callback() {
                    @Override
                    public void onSuccess(Object o) {
                        PaletteBean paletteBean=(PaletteBean)o;
                        if (paletteBean.getStatus()==1){
                            if(paletteBean.getData() != null && paletteBean.getData().size()>0){
                                page = 0;
                                list.clear();
                                list=paletteBean.getData();

                            }
                            else {
                                Toast.makeText(PaletteActivity.this,"刷新失败",Toast.LENGTH_LONG).show();
                            }

                        }
                        else {
                            Toast.makeText(PaletteActivity.this,"刷新失败",Toast.LENGTH_LONG).show();
                        }
                        palette_xrecyler.refreshComplete();
                    }

                    @Override
                    public void onFailure(ReturnStatusResultEntity error) {

                        if(list.size()>0) {
                               list.clear();
                                Toast.makeText(PaletteActivity.this, "刷新失败", Toast.LENGTH_LONG).show();

                            }
                    }
                });
            }

            @Override
            public void onLoadMore() {

            }
        });
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_goback:
                finish();

                break;
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
